﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

using LowLevelGraphics;
using LowLevelGraphics.Filter;

using ImageRecognition2.Forms;

using System.Windows.Forms;

namespace ImageRecognition2.Action
{
    /// <summary>
    /// Action for Brightness and Contrast
    /// </summary>
    public class ActionBrightnessContrast : AbstractCommand
    {
        protected BaseImageFilter m_FilterBrightness = new Brightness();
        protected BaseImageFilter m_FilterContrast = new Contrast();

        /// <summary>
        /// Initializes a new instance of the <see cref="ActionBrightnessContrast"/> class.
        /// </summary>
        public ActionBrightnessContrast()
        {
        }

        internal ActionBrightnessContrast(ActionBrightnessContrast _actionBrightnessContrast)
        {
            m_FilterBrightness = _actionBrightnessContrast.m_FilterBrightness;
            m_FilterContrast = _actionBrightnessContrast.m_FilterContrast;
        }

        /// <summary>
        /// Runs the specified sender.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        public override void Run(object sender, EventArgs e)
        {
            FormBrightnessContrast formFilter = new FormBrightnessContrast((Bitmap)MainFormSingleton.ActivePictureBox.BackgroundImage.Clone());
            formFilter.trackbAmount.Scroll += delegate(object senderTemp, EventArgs eTemp)
            {
                Execute(formFilter);
            };

            formFilter.tracbkContrast.Scroll += delegate(object senderTemp, EventArgs eTemp)
            {
                Execute(formFilter);
            };

            if (formFilter.ShowDialog() == DialogResult.OK)
            {
                Bitmap bitmap = (Bitmap)MainFormSingleton.ActivePictureBox.BackgroundImage;

                m_FilterBrightness = new Brightness(formFilter.trackbAmount.Value);
                m_FilterContrast = new Contrast((sbyte)formFilter.tracbkContrast.Value);

                UnsafeBitmap unsafeBitmap = m_FilterBrightness.Execute(bitmap);
                unsafeBitmap.Dispose();

                unsafeBitmap = m_FilterContrast.Execute(bitmap);
                unsafeBitmap.Dispose();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="formFilter"></param>
        private void Execute(FormBrightnessContrast formFilter)
        {
            Bitmap formFilterpictureBox1BackgroundImageClone = (Bitmap)formFilter.imgOriginal.Clone();
            BaseImageFilter baseImageFilter = null;
            m_FilterBrightness = (BaseImageFilter)Activator.CreateInstance(m_FilterBrightness.GetType(), new object[] { (int)formFilter.trackbAmount.Value });
            m_FilterContrast = (BaseImageFilter)Activator.CreateInstance(m_FilterContrast.GetType(), new object[] { (sbyte)formFilter.tracbkContrast.Value });
            baseImageFilter.ProcessingMode = PROCESSING_MODE.Color;
            baseImageFilter.Execute(formFilterpictureBox1BackgroundImageClone).Dispose();
            formFilter.pictureBox1.BackgroundImage = formFilterpictureBox1BackgroundImageClone;
        }

        public override object Clone()
        {
            return new ActionBrightnessContrast(this);
        } 
    }
}
